import { parseTime } from '@/utils/index'
export const download = {
  data() {
    return {
      appendName: '',
      // 导出相关 显示的表头键值对
      header: [], // 表头字段（值）
      headerKeys: [], // 表头字段（键） filterVal
      listData: [], // 表体数据
      filename: parseTime(new Date().getTime(), '{y}-{m}-{d}__{h}{i}'),
      autoWidth: true,
      bookType: 'xlsx'
    }
  },
  methods: {
      indexOfSecondE(str) {
      const regex = new RegExp("/[E,e]/g" ) ; // 创建一个匹配'E'的正则表达式
      let match;
      let count = 0;
      while ((match = regex.exec(str)) !== null) {
        count += 1;
        if (count === 2) {
          return match.index; // 返回第二个'E'的索引
        }
      }
      return -1; // 如果没有找到第二个'E'，返回-1
    },


    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          // console.log(v[j], 'v[j]  ', typeof v[j], isNaN(v[j]))
          if (v[j] !== '') {
            if (!isNaN(v[j])) {
              if(this.indexOfSecondE(v[j])){
                return  (v[j])
              }else{
                return Number(v[j])
              }
              
            } else {
              
              return v[j]
            }
          }
        }
      }))
    },
    exportData() {
      import('@/vendor/Export2Excel').then(excel => {
        const data = this.formatJson(this.headerKeys, this.listData)
        const routeTitle = this.$route.meta.title
        // console.log(this.filename.indexOf(routeTitle), '  xxx')
        // console.log(routeTitle, ' routeTitle')
        const index = this.filename.indexOf(routeTitle)
          console.log(this.filename,this.listData , ' this.filename')
        if (index < 0) {
          this.filename = this.filename + routeTitle
        } else {
          this.filename = this.filename
        }
        if (this.appendName) {
          this.filename = parseTime(new Date().getTime(), '{y}-{m}-{d}__{h}{i}') + routeTitle + this.appendName
          this.appendName = ''
        }
        excel.export_json_to_excel({
          header: this.header,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
      })
    },
    exportDetailData(title) {
      import('@/vendor/Export2Excel').then(excel => {
        const data = this.formatJson(this.headerKeys, this.listData)
        const routeTitle = this.$route.meta.title
        excel.export_json_to_excel({
          header: this.header,
          data,
          filename: parseTime(new Date().getTime(), '{y}-{m}-{d}__{h}{i}') + routeTitle + title,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
      })
    }

  }
}

